package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


/**
 * @Author Au
 * @DATE 2020/11/16 18:29
 **/
@Service(version = "1.0.0")
public class RecommendUserApiImpl implements RecommendUserApi{


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 今日佳人
     * @param userId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long userId) {
        Criteria criteria = Criteria.where("toUserId").is(userId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);


        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 推荐用户的用户列表查询
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<RecommendUser> queryPageInfo(Long userId, Integer pageNum, Integer pageSize) {
        Criteria criteria = Criteria.where("toUserId").is(userId);
        Pageable pageable = PageRequest.of(pageNum-1, pageSize, Sort.by(Sort.Order.desc("score")));

        Query query = Query.query(criteria).with(pageable);

        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return new PageInfo<RecommendUser>(0, pageNum, pageSize, recommendUsers);
    }

    @Override
    public double queryScore(Long userId, Long toUserId) {
        Query query = Query.query(Criteria
                .where("toUserId").is(toUserId) //1
                .and("userId").is(userId)); //1
        RecommendUser recommendUser = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (null == recommendUser) {
            return 0;
        }
        return recommendUser.getScore();
    }
}
